from .api import start, MODE_BUILD, MODE_FORCE, MODE_CLEAN
from .lib.cliopts import CliOpts, switch


class CommandLine(CliOpts):
    program_name = "Gabot Build System"
    program_version = "1.0"
    min_tail_args = 1
    max_tail_args = 1
    usage = "[OPTIONS] projectfile"
    
    default_configfile = ".config.gabot"
    configfile = None
    load_auto_config = True
    mode = MODE_BUILD
    
    @switch("m", "mode", args = [str])
    def sw_mode(self, mode):
        mode = mode.lower()
        if mode == "clean":
            self.mode = MODE_CLEAN
        elif mode == "build":
            self.mode = MODE_BUILD
        elif mode == "force":
            self.mode = MODE_FORCE
        else:
            raise ValueError("Invalid mode: %r" % (mode,))
    
    @switch("c", "config", args = [str])
    def sw_conf(self, filename):
        self.configfile = filename
    
    @switch(longname = "noconfig")
    def sw_noconf(self, mode):
        self.load_auto_config = False
    
    def main(self, projfile):
        if not self.configfile and self.load_auto_config:
            self.configfile = self.default_configfile
        if self.configfile:
            execfile(self.configfile)
        start(projfile, self.mode)


cli = CommandLine()



